package com.taiyuan.zfmvp.JUC.lock;

import org.apache.commons.lang3.RandomUtils;

import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption:
 * @Date 2022/10/2
 * @return
 */

class ticket{

    private  int number = 30;
    //创建可重入锁

    private final ReentrantLock lock = new ReentrantLock(true);

    public void sale(){

        //上锁
        lock.lock();
        try {
            //TimeUnit.MILLISECONDS.sleep(500);


            //执行逻辑
            if(number > 0){
               // System.out.println(Thread.currentThread().getName()+" 卖出："+(number--)+" 剩余"+number);
                System.out.println("testlock"+UUID.randomUUID().toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }

}

public class Lsaleticket {

    public static void main(String[] args) {
        try {
            ticket t = new ticket();

            for (int i = 0; i < 10000; i++) {

                t.sale();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*ticket t = new ticket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    t.sale();
                }
            }
        },"a").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    t.sale();
                }
            }
        },"b").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    t.sale();
                }
            }
        },"c").start();*/
    }

}
